package com.wcgksoft.controller.constructionTeam;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.comm.StringUtils;
import com.wcgksoft.entity.ConstructionTeam.ConstructionTeam;
import com.wcgksoft.entity.ConstructionTeam.TeamMembers;
import com.wcgksoft.entity.GreedLand.GreedLand;
import com.wcgksoft.entity.GreedLand.GreedLandAdministrator;
import com.wcgksoft.entity.system.*;
import com.wcgksoft.service.ConstructionTeam.ConstructionTeamService;
import com.wcgksoft.service.ConstructionTeam.TeamMembersService;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.GreedLand.GreedlandAdministratorService;
import com.wcgksoft.service.GreedLand.GreedlandService;
import com.wcgksoft.service.SysRoleCodeService;
import com.wcgksoft.service.system.EnterpriseService;
import com.wcgksoft.service.system.SysRoleService;
import com.wcgksoft.service.system.UserService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 周权
 * @Date: 2020/03/09/12:11
 * @Description:施工团队
 */

@Controller
@RequestMapping(value = "system/constructionTeam")
public class ConstructionTeamController {
    @Autowired
    private ConstructionTeamService constructionTeamService;
    @Autowired
    private UserService userService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private TeamMembersService teamMembersService;
    @Autowired
    private EnterpriseProjectService enterpriseProjectService;
    @Autowired
    private GreedlandAdministratorService greedlandAdministratorService;
    @Autowired
    private GreedlandService greedlandService;

    /*
     * @Author: 周权
     * @Description:施工团队列表页面layui的table加载数据
     * @Param:
     * @create: 2020/3/9 12:32
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "e_id") Integer e_id,
                         @RequestParam(value = "page", required = false) Integer page,
                         @RequestParam(value = "limit", required = false) Integer limit,
                         @RequestParam(value = "t_name",required = false) String t_name) {
        DetachedCriteria criteria = DetachedCriteria.forClass(ConstructionTeam.class);
        criteria.add(Restrictions.eq("e_id", e_id));
        criteria.add(Restrictions.eq("t_state", 0));
        if (StringUtils.isNotEmpty(t_name)){
            criteria.add(Restrictions.eq("t_name", t_name));
        }
        List<ConstructionTeam> byConditions = constructionTeamService.findByConditions(criteria,page,limit);
        return Layui.data(0, "查询成功", constructionTeamService.getTotalCount(), byConditions);
    }

    /*
    * @Author: 周权
    * @Description: 施工团队修改页面员工信息回显
    * @Param:
    * @create: 2020/3/9 21:40
    * @return:
    */
    @ResponseBody
    @RequestMapping(value = "findMember")
    public Layui findMember(@RequestParam(value = "tid") Integer tid) {
        DetachedCriteria criteria = DetachedCriteria.forClass(TeamMembers.class);
        criteria.add(Restrictions.eq("t_id", tid));
        List<TeamMembers> byConditions = teamMembersService.findByConditions(criteria);
        return Layui.data(0, "查询成功", teamMembersService.getTotalCount(), byConditions);
    }

    /*
     * @Author: 周权
     * @Description: 施工团队列表页面的添加/修改数据
     * @Param:
     * @create: 2020/3/9 15:07
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "update")
    @Transactional
    public Layui update(@RequestParam(value = "data", required = false) String data,
                        @RequestParam(value = "names")String names,
                        @RequestParam(value = "sexs")String sexs,
                        @RequestParam(value = "ages")String ages){
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            String id1 = String.valueOf(param.get("id"));
            if(null==id1||id1.equals("")){
                ConstructionTeam constructionTeam = (ConstructionTeam) JSONObject.toBean(JSONObject.fromObject(data), ConstructionTeam.class);
                constructionTeam.setT_createDate(new Date());
                constructionTeam.setT_code(1);
                constructionTeam.setT_state(0);
                constructionTeamService.save(constructionTeam);
                if (null!=names){
                    String[] namelist = names.split(",");
                    String[] sexlist = sexs.split(",");
                    String[] agelist = ages.split(",");
                    if (namelist.length>0){
                        for (int i = 0; i < namelist.length; i++) {
                            if ((null==namelist[i]||namelist[i].equals("")||namelist[i]=="")&&(null==sexlist[i]||sexlist[i].equals("")||sexlist[i]=="")&&(null==agelist[i]||agelist[i].equals("")||agelist[i]=="")){
                                continue;
                            }else{
                                TeamMembers teamMembers = new TeamMembers();
                                teamMembers.setT_id(constructionTeam.getId());
                                teamMembers.setM_createDate(new Date());
                                teamMembers.setM_name(namelist[i]);
                                teamMembers.setM_sex(sexlist[i]);
                                teamMembers.setM_age(agelist[i]);
                                teamMembersService.save(teamMembers);
                            }

                        }
                    }

                }
                return Layui.data(0, "添加成功", 1, Layui.addList(constructionTeam));
            }else{
                ConstructionTeam constructionTeam = (ConstructionTeam) JSONObject.toBean(JSONObject.fromObject(data), ConstructionTeam.class);
                constructionTeam.setT_createDate(new Date());
                constructionTeam.setT_state(0);
                constructionTeamService.saveOrUpdate(constructionTeam);
                if (null!=names){
                    DetachedCriteria criteria = DetachedCriteria.forClass(TeamMembers.class);
                    criteria.add(Restrictions.eq("t_id", constructionTeam.getId()));
                    List<TeamMembers> byConditions = teamMembersService.findByConditions(criteria);
                    if(null!=byConditions){
                        for(TeamMembers teamMembers:byConditions){
                            teamMembersService.deleteCass(teamMembers);
                        }
                        String[] namelist = names.split(",");
                        String[] sexlist = sexs.split(",");
                        String[] agelist = ages.split(",");
                        if (namelist.length>0){
                            for (int i = 0; i < namelist.length; i++) {
                                if ((null==namelist[i]||namelist[i].equals("")||namelist[i]=="")&&(null==sexlist[i]||sexlist[i].equals("")||sexlist[i]=="")&&(null==agelist[i]||agelist[i].equals("")||agelist[i]=="")){
                                    continue;
                                }else{
                                    TeamMembers teamMembers = new TeamMembers();
                                    teamMembers.setT_id(constructionTeam.getId());
                                    teamMembers.setM_createDate(new Date());
                                    teamMembers.setM_name(namelist[i]);
                                    teamMembers.setM_sex(sexlist[i]);
                                    teamMembers.setM_age(agelist[i]);
                                    teamMembersService.save(teamMembers);
                                }
                            }
                        }
                    }else{
                        String[] namelist = names.split(",");
                        String[] sexlist = sexs.split(",");
                        String[] agelist = ages.split(",");
                        if (namelist.length>0){
                            for (int i = 0; i < namelist.length; i++) {
                                if ((null==namelist[i]||namelist[i].equals("")||namelist[i]=="")&&(null==sexlist[i]||sexlist[i].equals("")||sexlist[i]=="")&&(null==agelist[i]||agelist[i].equals("")||agelist[i]=="")){
                                    continue;
                                }else{
                                    TeamMembers teamMembers = new TeamMembers();
                                    teamMembers.setT_id(constructionTeam.getId());
                                    teamMembers.setM_createDate(new Date());
                                    teamMembers.setM_name(namelist[i]);
                                    teamMembers.setM_sex(sexlist[i]);
                                    teamMembers.setM_age(agelist[i]);
                                    teamMembersService.save(teamMembers);
                                }
                            }
                        }
                    }
                }
                return Layui.data(0, "修改成功", 1, Layui.addList(constructionTeam));
            }
        }
    }

    /*
    * @Author: 周权
    * @Description:施工管理账号添加
    * @Param:
    * @create: 2020/3/9 14:24
    * @return:
    */
    @ResponseBody
    @RequestMapping(value = "setAdmin")
    public Layui setAdmin(@RequestParam(value = "data", required = false) String data){
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            DetachedCriteria criteria = DetachedCriteria.forClass(SysRole.class);
            String e_id = String.valueOf(param.get("e_id"));
            int i = Integer.parseInt(e_id);
            Enterprise byId = enterpriseService.findById(i);
            criteria.add(Restrictions.eq("enterprise.id", i));
            criteria.add(Restrictions.eq("sysRoleCode.id", 4));
            List<SysRole> byConditions = sysRoleService.findByConditions(criteria);
            if(byConditions.size()>0){
                String team_id = String.valueOf(param.get("team_id"));
                int i1 = Integer.parseInt(team_id);
                ConstructionTeam byId2 = constructionTeamService.findById(i1);
                String projectid = String.valueOf(param.get("projectid"));
                int project = Integer.parseInt(projectid);
                String areaid = String.valueOf(param.get("areaid"));
                int area = Integer.parseInt(areaid);
                byId2.setT_project_id(project);
                byId2.setT_area_id(area);
                byId2.setT_code(0);
                byId2.setT_state(0);
                constructionTeamService.saveMerge(byId2);

                SysUser sysUser = (SysUser) JSONObject.toBean(JSONObject.fromObject(data), SysUser.class);
                sysUser.setEnterprise(byId);
                sysUser.setCreateDate(new Date());
                sysUser.setProId(project);
                sysUser.setStatus(1);//施工账号未冻结状态
                sysUser.setConstructionTeam(byId2);//设置账号所属队伍
                for(int j = 0;j<byConditions.size();j++){
                    sysUser.setSysRole(byConditions.get(0));//关联角色中间表
                }
                userService.save(sysUser);
                return Layui.data(0, "添加成功", 1, Layui.addList(sysUser));
            }else{
                return Layui.data(1, "您还未添加施工角色,请到角色管理中添加施工角色后再创建施工账号",null);
            }
        }
    }

    /*
     * @Author: 周权
     * @Description:施工团队删除功能
     * @Param:
     * @create: 2020/3/9 10:20
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id") Integer id) {
        ConstructionTeam byId = constructionTeamService.findById(id);
        byId.setT_state(1);//设置施工团队状态为1 在列表页隐藏
        constructionTeamService.saveOrUpdate(byId);

        DetachedCriteria criteria = DetachedCriteria.forClass(SysUser.class);
        criteria.add(Restrictions.eq("team_id", id));
        List<SysUser> byConditions = userService.findByConditions(criteria);
        if(byConditions.size()>0){
            for(int i = 0;i < byConditions.size();i++){
                byConditions.get(i).setStatus(2);//设置施工账号状态不为1 不允许登录
                userService.saveOrUpdate(byConditions.get(i));
            }
        }
        return Layui.data(1, "删除成功", 1, null);
    }

    /*
    * @Author: 周权
    * @Description:查询当前企业账号所有项目信息
    * @Param:
    * @create: 2020/3/15 15:48
    * @return:
    */
    @ResponseBody
    @RequestMapping(value = "findProject")
    public Layui findProject(@RequestParam(value = "e_id") Integer e_id) {
        DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseProject.class);
        criteria.add(Restrictions.eq("enterprise.id", e_id));
        criteria.add(Restrictions.eq("parentId", -1));
        List<EnterpriseProject> byConditions = enterpriseProjectService.findByConditions(criteria);
        return Layui.data(1, "查询成功", enterpriseProjectService.getTotalCount(), byConditions);
    }

    /*
     * @Author: 周权
     * @Description:通过项目ID查询项目下所有片区
     * @Param:
     * @create: 2020/3/15 15:48
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "findArea")
    public Layui findArea(@RequestParam(value = "areaid") Integer areaid,
                          @RequestParam(value = "e_id") Integer e_id) {
        DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseProject.class);
        criteria.add(Restrictions.eq("enterprise.id", e_id));
        criteria.add(Restrictions.eq("parentId", areaid));
        List<EnterpriseProject> byConditions = enterpriseProjectService.findByConditions(criteria);
        return Layui.data(1, "查询成功", enterpriseProjectService.getTotalCount(), byConditions);
    }

    /*
     * @Author: 周权
     * @Description:通过片区ID查询片区下所有绿地
     * @Param:
     * @create: 2020/3/15 15:48
     * @return:
     */
    @ResponseBody
    @RequestMapping(value = "findGreenland")
    public Layui findGreenland(@RequestParam(value = "areaid") Integer areaid
    ) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLand.class);
        criteria.add(Restrictions.eq("state", 0));
        String s = String.valueOf(areaid);
        criteria.add(Restrictions.eq("areaid", s));
        List<GreedLand> byConditions = greedlandService.findByConditions(criteria);
        return Layui.data(1, "查询成功", greedlandService.getTotalCount(), byConditions);
    }

    /*
    * @Author: 周权
    * @Description:查找片区/企业下所有施工队
    * @Param:
    * @create: 2020/3/31 10:11
    * @return:
    */
    @ResponseBody
    @RequestMapping(value = "findAreaTeam")
    public Layui findAreaTeam(@RequestParam(value = "areaid", required = false) Integer areaid,
                              @RequestParam(value = "eid") Integer eid,
                              @RequestParam(value = "roleid") Integer roleid) {
        DetachedCriteria criteria = DetachedCriteria.forClass(ConstructionTeam.class);
        criteria.add(Restrictions.eq("t_state", 0));
        if(roleid==3){
            criteria.add(Restrictions.eq("t_area_id", areaid));
        }
        criteria.add(Restrictions.eq("e_id", eid));
        List<ConstructionTeam> byConditions = constructionTeamService.findByConditions(criteria);
        return Layui.data(1, "查询成功", constructionTeamService.getTotalCount(), byConditions);
    }
}
